package com.beep.erp.model.stockdetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.BizStorageLocationsListBean;
import com.beep.erp.common.bean.StockListBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.recyclerview.adapter.HeaderAndFooterRecyclerViewAdapter;
import com.beep.erp.common.widget.recyclerview.divider.HorizontalDividerItemDecoration;
import com.beep.erp.common.widget.recyclerview.utils.RecyclerViewUtils;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.common.widget.tips.TipsHelper;
import com.beep.erp.model.goodsdetail.GoodsDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 采购单详情页
 */
public class StockDetailActivity extends ToolbarActivity<StockDetailPresenter, StockDetailModel> implements StockDetailContract.View, RefreshView.OnFreshListener,
        StockListAdapter.OnItemClickLitener, InStockListAdapter.onInStockClickLitener {
    private static final String PURCHASEID = "purchaseId:";

    @BindView(R.id.activity_invite)
    CoordinatorLayout mCoordinatorLayout;
    @BindView(R.id.rlv_mine_invite)
    RecyclerView rlv_mine_invite;
    @BindView(R.id.super_refresh)
    RefreshView refreshView;
    @BindView(R.id.recycler_view_instock)
    RecyclerView rlViewInstock;
    @BindView(R.id.rl_instock_header)
    RelativeLayout header;

    private TextView tvNo;

    TipsHelper mTipsHelper;
    private ArrayList<StockListBean.BizPurchaseDetailVoListBean> mListBeen = new ArrayList<>();
    private ArrayList<StockListBean.BizStockInVoListBean> mInStockList = new ArrayList<>();
    private StockListAdapter mStockListAdapter;
    private InStockListAdapter mInStockListAdapter;

    public static void start(Context context, String purchaseId) {
        Intent intent = new Intent(context, StockDetailActivity.class);
        intent.putExtra(PURCHASEID, purchaseId);
        context.startActivity(intent);
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_stock_detail;
    }

    @Override
    protected void initView() {
        getTvTitle().setText(R.string.stock_detail);
        mTipsHelper = createTipsHelper(refreshView);
        //refreshView下拉刷新 上拉加载
        refreshView.setListener(this);
        refreshView.setHeader(new DefaultHeader(this, true));
        //mInviteAdapter recycleview适配器
        mStockListAdapter = new StockListAdapter(this, mListBeen);
        //设置为纵向
        rlv_mine_invite.setLayoutManager(new LinearLayoutManager(this));
        //必写
        rlv_mine_invite.setHasFixedSize(true);
        //添加分割线
        rlv_mine_invite.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
        //setAdapter  rlv_mine_invite 这儿用new HeaderAndFooterRecyclerViewAdapter(mInviteAdapter) 是 添加了头布局 直接 写setAdapter(mInviteAdapter)
        rlv_mine_invite.setAdapter(new HeaderAndFooterRecyclerViewAdapter(mStockListAdapter));

        //入库单
        mInStockListAdapter = new InStockListAdapter(this, mInStockList);
        //设置为纵向
        rlViewInstock.setLayoutManager(new LinearLayoutManager(this));
        //必写
        rlViewInstock.setHasFixedSize(true);
        //添加分割线
        rlViewInstock.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
        //setAdapter  rlv_mine_invite 这儿用new HeaderAndFooterRecyclerViewAdapter(mInviteAdapter) 是 添加了头布局 直接 写setAdapter(mInviteAdapter)
        rlViewInstock.setAdapter(new HeaderAndFooterRecyclerViewAdapter(mInStockListAdapter));


        //头布局
        View view = LayoutInflater.from(this).inflate(R.layout.stock_detail_header_item, null);
        tvNo = (TextView) view.findViewById(R.id.tv_no);
        RecyclerViewUtils.setHeaderView(rlv_mine_invite, view);

        mStockListAdapter.setOnItemClickLitener(this);
        mInStockListAdapter.setonInStockClickLitener(this);

//        RecyclerViewUtils.setHeaderView(rlViewInstock, LayoutInflater.from(this).inflate(R.layout.instock_detail_header_item, null));
    }

    @Override
    public void getDate() {
        //请求数据
        mListBeen.clear();
        mInStockList.clear();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideError();
        mTipsHelper.showLoading(true);
        getStockDetail();
    }

    private void getStockDetail() {
        long mills = TimeUtils.getCurTimeMills();
        Map<String, String> map = new HashMap<>();
        map.put("purchaseId", getIntent().getStringExtra(PURCHASEID));
        mPresenter.getStockDetail(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", getIntent().getStringExtra(PURCHASEID));
    }

    @Override
    public void getStockDetail(StockListBean stockListBean) {
        //请求成功
        mListBeen.addAll(stockListBean.getBizPurchaseDetailVoList());
        mInStockList.addAll(stockListBean.getBizStockInVoList());
        tvNo.setText(stockListBean.getPurchaseId());
        mStockListAdapter.notifyDataSetChanged();
        mInStockListAdapter.notifyDataSetChanged();
        header.setVisibility(ToolsUtils.isEmptyList(stockListBean.getBizStockInVoList()) ? View.GONE : View.VISIBLE);
        if (stockListBean.getBizPurchaseDetailVoList().isEmpty()) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

    @Override
    public void getBizStorageLocationList(BizStorageLocationsListBean bizStorageLocationsListBean) {

    }

    @Override
    public void saveInstock() {

    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        mTipsHelper.hideLoading();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideEmpty();
        mTipsHelper.hideError();
        refreshView.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        //错误信息
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> getDate());
        } else {
            ToastUtils.showShort(message);
        }
    }

    //刷新
    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {

    }

    @Override
    public void onItemClick(View view, int position) {
        GoodsDetailActivity.start(this, mListBeen.get(position).getGoodsId());
    }

    @Override
    public void onInStockClick(View view, int position) {

    }
}
